<template>
  <div class="tool_container">
    <div class="toolli">
      <van-button icon="photograph" plain @click="handlecamera">拍照</van-button>
      <van-button class="toolright" icon="scan" plain @click="toscancode">清点</van-button>
    </div>
    <div class="toolli">
      <van-button icon="todo-list" plain @click="toborrow">外借</van-button>
      <van-button class="toolright" icon="phone-circle" @click="torepair" plain>修复</van-button>
    </div>
    <div class="toolli">
      <van-button icon="ascending" plain @click="tooutside">出库</van-button>
      <van-button class="toolright" icon="descending" plain @click="toinner">入库</van-button>
    </div>
    <!-- <img :src="baseurl+imgurl" alt=""> -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      showloading: "",
      crno: [],
      imgurl: [],
      twocode: [],
      token: "",
      baseurl: "http://zwyzy.vipgz6.idcfengye.com/"
      // baseurl:"http://crms.ngrok2.xiaomiqiu.cn"
    };
  },
  methods: {
    // 拍照生成图片上传
    handlecamera() {
      wx.chooseImage({
        count: 9,
        sizeType: ["original", "compressed"],
        sourceType: ["album", "camera"],
        success: result => {
          console.log(result);
          var tempFilePaths = result.tempFilePaths;
          // console.log(tempFilePaths);
          for (var i = 0; i < tempFilePaths.length; i++) {
            // console.log(tempFilePaths);
            var tempFilePath = tempFilePaths[i];
            let token = wx.getStorageSync("token");
            wx.uploadFile({
              url: this.baseurl + "import_crifo/import_photo",
              filePath: tempFilePath,
              header: {
                "content-type": "multipart/form-data",
                token: token
              },
              name: "file",
              formData: {
                method: "POST" //请求方式
              },
              success: res => {
                console.log("hhh");
                console.log(res);
                var data = JSON.parse(res.data);
                // var data = res.data
                console.log(res);
                console.log(data.data);
                // console.log(data.data[0].twoCodeImage);
                var str = data.data[0].twoCodeImage.replace(/[\r\n]/g, "");
                var image = data.data[0].image;
                var crno = data.data[0].crNo;
                this.twocode.push(str);
                this.imgurl.push(image);
                this.crno.push(crno);
                wx.setStorageSync("twocode", this.twocode);
                wx.setStorageSync("imgurl", this.imgurl);
                wx.setStorageSync("crNo", this.crno);
              },
              fail: error => {
                wx.showToast({
                  icon: "none",
                  duration: 2500,
                  title: "操作失败，请重试"
                });
                console.log(error);
                return;
              }
            });
          }
          wx.showToast({
            title: "提交成功",
            duration: 2500
          });
          wx.navigateTo({
            url: "/pages/twocode/main"
          });
        },
        fail: err => {
          wx.showToast({
            title: "取消了选择图片"
          });
        }
      });
    },
    // 清点
    toscancode() {
      // 1 跳转到选择位置页面
      wx.navigateTo({
        url: "/pages/choseaddr/main",
        success: result => {
          console.log(result);
        }
      });
      // 2 扫码
      // wx.scanCode({
      //   onlyFromCamera: false,
      //   scanType: ['qrCode','barCode','datamatrix','pdf417'],
      //   success: (result)=>{
      //     console.log(result);
      //     // 跳转到修复页面
      //     wx.navigateTo({
      //       url: '/pages/repairput/main',
      //       success: (result)=>{
      //         console.log(result);
      //       },
      //     });
      //   },
      //   fail: (err)=>{
      //     wx.showToast({
      //       title: '扫码失败',
      //       icon: 'none'
      //     });
      //   }
      // })
    },
    // 外借表
    toborrow() {
      wx.scanCode({
        onlyFromCamera: false,
        scanType: ["qrCode", "barCode", "datamatrix", "pdf417"],
        success: result => {
          console.log(result);
          var str = result.result.lastIndexOf("/");
          console.log(result.result.length);
          console.log(str);
          var id = result.result.substring(str + 1, result.result.length);
          console.log("hhhhhh");
          wx.navigateTo({
            url: "/pages/borrow/main?id="+id
          });
        },
        fail: err => {
          wx.showToast({
            title: "扫码失败",
            icon: "none"
          });
        }
      });
    },
    // 修复
    torepair() {
      wx.scanCode({
        onlyFromCamera: false,
        scanType: ["qrCode", "barCode", "datamatrix", "pdf417"],
        success: result => {
          console.log(result);
          var str = result.result.lastIndexOf("/");
          console.log(result.result.length);
          console.log(str);
          var id = result.result.substring(str + 1, result.result.length);
          console.log("hhhhhh");
          wx.navigateTo({
            url: "/pages/repairput/main?id="+id
          });
        },
        fail: err => {
          wx.showToast({
            title: "扫码失败",
            icon: "none"
          });
        }
      });
    },
    // 出库
    tooutside() {
      wx.scanCode({
        onlyFromCamera: false,
        scanType: ["qrCode", "barCode", "datamatrix", "pdf417"],
        success: result => {
          console.log(result);
          var str = result.result.lastIndexOf("/");
          console.log(result.result.length);
          console.log(str);
          var id = result.result.substring(str + 1, result.result.length);
          wx.navigateTo({
            url: "/pages/outside/main?id="+id
          });
        },
        fail: err => {
          wx.showToast({
            title: "扫码失败",
            icon: "none"
          });
        }
      });
    },
    // 入库
    toinner() {
      wx.scanCode({
        onlyFromCamera: false,
        scanType: ["qrCode", "barCode", "datamatrix", "pdf417"],
        success: result => {
          console.log(result);
          var str = result.result.lastIndexOf("/");
          console.log(result.result.length);
          console.log(str);
          var id = result.result.substring(str + 1, result.result.length);
          this.edittoin(id);
          console.log("hhhhhh");
        },
        fail: err => {
          wx.showToast({
            title: "扫码失败",
            icon: "none"
          });
        }
      });
    },
    // 修改为入库
    async edittoin(id) {
      const res = await this.$http.put({
        url: "crms/crmsAdmin/updateStatus?crNo=" + id
      });
      if (res.code !== 200) {
        return wx.showToast({
          title: "修改失败",
          icon: "none"
        });
      }
      wx.showToast({
        title: res.data,
        icon: "none",
        duration: 1500
      });
      console.log(res);
    }
  }
};
</script>

<style>
page {
  height: 100%;
  background: #f3f3f3;
}
.tool_container {
  display: flex;
  height: 100%;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.van-button {
  height: 100rpx;
  width: 250rpx;
  border-radius: 40rpx !important;
}

.toolli {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 60rpx;
}
.toolright {
  margin-left: 50rpx;
}
</style>